<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 Jindai.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Http\Resources;

use App\Traits\ResourceTrait;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Class Song
 * @package App\Http\Resources
 *
 * @mixin \App\Models\Song
 */
class Song extends JsonResource
{
    use ResourceTrait;

    /**
     * @param $request
     * @return array
     *
     * @SWG\Definition(
     *     definition="songDefault",
     *     type="object",
     *     @SWG\Property(property="id", type="integer", description="id", example=1),
     *     @SWG\Property(property="name", type="string", description="名称", example="计算机网络"),
     *     @SWG\Property(property="created_at", type="integer", description="创建时间", example=1569292862),
     *     @SWG\Property(property="deleted_at", type="integer", description="创建时间", example=1569292862)
     * )
     */
    protected function toDefault($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'song_id' => $this->song_id,
            'song_images' => $this->song_images,
            'singer_id' => $this->singer_id,
            'singer' => $this->singer,
            'tags' => $this->tags,
            'album_name' => $this->album_name,
            'album_id' => $this->album_id,
            'album_images' => $this->album_images,
            'content_id' => $this->content_id,
            'copyright_id' => $this->copyright_id,
            'invalidate' => $this->invalidate,
            'lrc_url' => $this->lrc_url,
            'mrc_url' => $this->mrc_url,
            'top_quality' => $this->top_quality,
            'top_url' => $this->top_url,
            'rate_formats' => $this->rate_formats,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'deleted_at' => $this->deleted_at,
        ];
    }

    /**
     * 歌单编辑
     *
     * @param $request
     * @return array
     */
    protected function toMenuEdit($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'song_id' => $this->song_id,
            'singer' => $this->singer,
            'album_name' => $this->album_name,
        ];
    }

    /**
     * @param $request
     * @return array
     *
     * @SWG\Definition(
     *     definition="songIndex",
     *     type="object",
     *     allOf = {
     *          @SWG\Schema(
     *              @SWG\Property(property="id", type="integer", description="id", example=1),
     *              @SWG\Property(property="name", type="string", description="名称", example="计算机网络")
     *          )
     *     }
     * )
     */
    protected function toIndex($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name
        ];
    }

    /**
     * @param $request
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     *
     * @SWG\Definition(
     *     definition="songInfo",
     *     type="object",
     *     @SWG\Property(property="id", type="integer", description="id", example=1),
     *     @SWG\Property(property="name", type="string", description="名称", example="计算机网络"),
     *     @SWG\Property(property="created_at", type="integer", description="创建时间", example=1569292862),
     *     @SWG\Property(property="deleted_at", type="integer", description="创建时间", example=1569292862)
     * )
     */
    protected function toInfo($request)
    {
        $requestHandle = app()->make(\App\Support\Request::class);
        if ($lrcUrl = $this->getLrcUrl()) {
            $lyric = $requestHandle->guzzleHttpRequest('GET', $lrcUrl, [], ['basic' => true]);
        } else {
            $lyric = [];
        }
        $pathInfo = parse_url($this->top_url);
        return [
            'name' => $this->name,
            'image' => current($this->song_images),
            'singer' => $this->singer,
            'album_name' => $this->album_name,
            'album_image' => current($this->album_images),
            'lyric' => $lyric,
            'url' => $pathInfo['path'],
            'host' => config('website.music.host'),
            'full_url' => $this->getSongUrl()
        ];
    }
}
